<!--
  入库明细
-->
<template>
	<a-modal title="入库记录" :width="1400" :visible="visible" :confirmLoading="confirmLoading" :footer="null"
		@ok="handleSubmit" @cancel="handleCancel">
		<a-card :bordered="false">
			<s-table ref="table" size="default" :columns="columnsDynamic" :data="loadData" :alert="true"
				:rowKey="(record) => record.code"
				:rowSelection="null && { selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :showPagination="false">
				<template slot="action" slot-scope="text, record">
					<a @click="$refs.entryForm.readonly(record)">详情</a>
				</template>
			</s-table>
		</a-card>
		<entry-form ref="entryForm"></entry-form>
	</a-modal>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { STable } from '@/components'
import { page, } from '@/api/modular/main/emsorder/emsOrderManage'
import inStoreSourceForm from '@/views/main/emsordermaterial/purchaseOrder/emsInStoreSourceForm'

import entryForm from './entryForm.vue'

export default {
	components: {
		STable,
		inStoreSourceForm,
		entryForm
	},

	data() {
		return {
			title: null,
			advanced: false, // 高级搜索 展开/关闭
			queryParam: {},  // 查询参数
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				return page(Object.assign(parameter, this.queryParam)).then((res) => {
					return res.data
				})
			},
			selectedRowKeys: [],
			selectedRows: [],
			defaultExpandedKeys: [],
			visible: false,
			confirmLoading: false,
			emsOrder: null,
			viewStateEnum: {
				normalSupplier: 'normalSupplier',
				primarySupplier: 'primarySupplier',
				purchaseOrderEnd: 'purchaseOrderEnd', //这个应该没有，UI和normalSupplier长的一样
			},
			viewState: '',
			extra: {},
		}
	},
	computed: {
		token() {
			return Vue.ls.get(ACCESS_TOKEN)
		},
		columnsDynamic() {
			let columns = [
				{
					title: '序号', width: '80px',
					align: 'center',
					customRender: (text, record, index) => `${index + 1}`,
				},
				{
					title: '入库单号',
					dataIndex: 'orderNumber'
				},			
				{
					title: '创建时间',
					dataIndex: 'createTime'
				},
				{
					title: '入库时间',
					dataIndex: 'rkTime'
				},
				{
					title: '操作人',
					dataIndex: 'createUserName',
				},
				{
					title: '备注',
					dataIndex: 'remark',
				},
				{
					title: '操作',
					dataIndex: 'action',
					scopedSlots: { customRender: 'action' }
				},
			]
			return columns
		},
	},
	methods: {
		show(record, extra) {
			console.log(record,extra);
			console.log(this.$businessEnum.ORDER_TYPE_ENUM.inOrder.id);
			
			this.visible = true
			this.emsOrder = record
			this.extra = extra

			// this.queryParam.pageSize = -1
			this.queryParam.orderId = record.orderId
			this.queryParam.orderType = this.$businessEnum.ORDER_TYPE_ENUM.inOrder.id
			this.queryParam.materialType = extra.materialType
			this.queryParam.sourceOrderId = record.orderId
			setTimeout(() => {
				this.handleOk()
				this.$refs.table.clearSelected()
			}, 100)
		},
		handleOk() {
			this.$refs.table.refresh()
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
		handleSubmit() {
		},
		handleCancel() {
			this.visible = false
		},
		onEmsMaterialIndex1Selected(materialRow) {
			// console.log(`选择了原料:id=${materialRow.materialId}, code=${materialRow.materialCode}, name=${materialRow.materialName}`)
			// debugger
			let { materialId, materialName, materialCode, materialType, costPrice } = materialRow
			// 需要调用接口往订单中添加物料
			let _emsOrder = Object.assign({}, this.emsOrder)
			_emsOrder.orderMaterials = []
			let orderMaterial = {
				orderMaterialId: '',
				orderId: _emsOrder.orderId,
				bomId: '0',  // 单独选择原料时没有BOMID
				materialId: materialId,
				number: 1,
				completedNumber: 0,
				executeStatus: '0',
				executeProgress: '0',
				status: '0',
				createTime: '',
				createUser: '',
				updateTime: '',
				updateUser: '',
			}
			_emsOrder.orderMaterials.push(orderMaterial)
			add(orderMaterial).then(res => {
				if (res.success) {
					this.$message.success('新增成功')
					// this.handleCancel()
					// this.$emit('ok', values)
					this.$refs.table.refresh()
				} else {
					this.$message.info('新增失败：' + res.message)
				}
			})
			// 然后刷新table控件
		},
		imageURLFormat(text) {
			return `${this.$store.state.baseUrl}/sysFileInfo/preview?id=${text}&token=${this.token}`
		}
	}
}
</script>

<style lang="less">
.table-operator {
	margin-bottom: 18px;
}

button {
	margin-right: 8px;
}
</style>
